{include file="common/meta" /}
{include file="common/script" /}

<div class="container" style="min-height: 1000px">
	<br>
	<!-- 面包屑路径导航 -->
		<ol class="breadcrumb hidden-xs">
			<li><a href="{:addon_url('leescore/index/index')}">{:__('store title')}</a></li>
			<li class="active">{:__('order pay page')}</li>
		</ol>
	<!-- 面包屑路径导航 -->
	<div class="row">
		<!-- 开启了订单清理 -->
		{if($config['open_clear'] == 1)}
			<div class="col-xs-12">
				<div class="alert alert-danger">
					{:__('pay timeout tips', array($config['open_clear_time']))}
				</div>
			</div>
		{/if}
		<form action="" name="postorder" class="postorder" method="post" id="postorder">
			{:token('__token__', 'sha1')}
			<!-- 确认订单信息 -->
			<div class="col-sm-12">
				<div class="box-body">
					<div class="row">
						<div class="col-md-7">
							<div> 
								<?php 
									$order_goods = $item->order_goods;
								?>
								<h3>{:__('List of goods')}</h3>
								<div class="goods-box bg-grays padding h5">
									<ul>
										{volist name="$order_goods" id="vo"}
											<li style="height: 60px;" class="hidden-xs">
												<div class="col-sm-2">
													<img src="{$vo.goods_thumb}" alt="{$vo.goods_name}" class="img-responsive" style="width: 60px; max-height: 60px; border-radius: 3px;">
												</div>
												<div class="col-sm-4 more">
													{$vo.goods_name}
												</div>
												<div class="col-sm-2">
													*{$vo.buy_num|default="0"}
												</div>
												<div class="col-sm-2">
													{$vo.score|default="0"}{:__('score')}
												</div>
												<div class="col-sm-2">
													{$vo.money|default="0.00"}{:__('money')}
												</div>
											</li>
										{/volist}
										{volist name="$order_goods" id="vo"}
											<li class="visible-xs">
												<div class="col-xs-4">
													<img src="{$vo.goods_thumb}" alt="{$vo.goods_name}" class="img-responsive" style="width: 60px; max-height: 60px; border-radius: 3px;">
												</div>
												<div class="col-xs-8 more"> 
													{$vo.goods_name} *{$vo.buy_num|default="0"}
													<br>
													<br>
													{$vo.score|default="0"}{:__('score')}
													<br>
													<br>
													{$vo.money|default="0.00"}{:__('money')}
												</div>
											</li>
										{/volist}
										<div class="clearfix"></div>
									</ul>
								</div>
								<br>
								<div class="text-right bg-grays padding">
									<div class="pull-left"><h2>{:__('total')}</h2></div>
									<span class="h2 text-warning">{$item.score_total} <small>{:__('score')}</small></span>
									<br> 
									<br>
									<span class="h2 text-danger">{$item.money_total} <small>{:__('money')}</small></span> 
									<div class="clearfix"></div>
									<br>
									<span>{if($user['score'] < $item.score_total)}{:__('miss point', ($item.score_total - $user['score']))}{/if}</span>
								</div>

								{if($item.money_total > 0)}
									<br>
									<br>
									<h3>支付方式：</h3>
									<ul>
										<li>
											<ul>
												{if($user['score'] < $item.score_total)}
													<li class="padding pull-left" style="border:2px #eee solid; border-radius: 4px; background-color: #fff; color: #ccc;">{:__('min score')}</li>
												{else}
													<h3>余额支付</h3>
												{/if}
											</ul>
										</li>
									</ul>
								{else}
									<input type="radio" value="score" class="hidden" name="paytype" checked="checked"> 
								{/if}
							</div>
							<div class="clearfix"></div>
							<div class="padding">
								<h3>{:__('order other')}</h3>
								<textarea name="other" id="other" class="form-control" rows="8"></textarea>
							</div>
							<br>	
						</div>
						
						<div class="col-md-5">
							<h3>{:__('address list')} <a href="javascript:;" class="btn btn-sm btn-warning insert-address pull-right insert-address">{:__('add')}{:__('address')}</a></h3>
							<br>
							<ul class="goods-order-address">
								{if(empty($address))}
									<li>
										{:__('empty')}
									</li>
								{/if}
								{volist name="address" id="vi"}
									<li {if($vi.status == 1)}class="margin-bottom item active" {else /}class="margin-bottom item"{/if} data-param="{$vi.id}">
										<div class="col-sm-10 col-xs-10 padding">
											<ul>
												<li class="pull-left">
													{:__('zip code')}：{$vi.zip} {:__('mobile')}：{$vi.mobile}  {:__('consignee')}：{$vi.firstname} {$vi.lastname}
													<p class="padding-top h5 bg-default">{:__('address')}：{$vi.country} {$vi.region} {$vi.city} {$vi.address}</p>
												</li>
											</ul>
											<div class="clearfix"></div>
										</div>
										<div class="col-xs-2 no-padding">
											<a href="javascript:;" data-id="{$vi.id}" style="height: 96px; line-height: 85px;" class="btn btn-flat btn-info btn-block edit-btn">{:__('edit')}</a>
										</div>
										
										<div class="clearfix"></div>
									</li>
								{/volist}
								<input type="hidden" value="{$item['id']}" name="oid" id="oid" />

								<input type="hidden" value="" name="address_id" id="address_id" />
							</ul>
						</div>	
					</div>
				</div>
			</div>
			<!-- ./ 确认订单信息 -->
		</form>
	</div>
</div>
<div class="postFixed">
	<div class="container text-center">
		{if($item['score_total'] <= $user['score'])}
			<a href="javascript:;"  data-param="{$item.id}" id="order_pay" class="btn btn-lg btn-flat btn-warning {if($user['score'] < $item.score_total || $user['money'] < $item.money_total)}disabled{/if}">
				{if($user['score'] < $item.score_total)}{:__('min score')}{else} {if($user['money'] < $item.money_total)}余额不足{else}{:__('replace pay')}{/if}{/if}
			</a>
		{/if}
	</div>
</div>

<!-- Layer -->
<script src="__CDN__/assets/libs/fastadmin-layer/dist/layer.js"></script>
<script>
	$(document).ready(function() {
		var active = $(".goods-order-address .active");

		if(active.length < 1)
		{
			$('.goods-order-address .item').eq(0).addClass('active');
		}

		$("#address_id").val($('.goods-order-address .active').attr("data-param"));

		//支付方式
/* 		$(".pay").on('click', function() {
			$("input[type=radio]").removeAttr('checked');
			$(".pay").removeClass('pay-active');
			$(this).addClass('pay-active');
			$(this).children("input[type=radio]").prop("checked","checked");
		}); */
		
		//核对收货地址数量最多10个
		var bwith = $("body").width();
		var w = bwith < 768 ? '100%' : '65%';
		$('.insert-address').on('click', function() {
			$.ajax({
				url: '{:addon_url("leescore/address/check")}',
				type: 'get',
				dataType: 'json',
				
				success: function (result)
				{
					if(result >= 10) return false;
					parent.layer.open({
						type: 2,
						title: '{:__("address")}{:__("add")}',
						cancel: function(){
							window.location.reload();
						},
						shadeClose: true,
						shade: 0.7,
						area: [w, w],
						content: '{:addon_url("leescore/address/add")}' //iframe的url
					});
				}
			});
		});

		//提交订单
		$("#order_pay").on('click', function() {
			var other = $("#other").val();
			var aurl = "{:addon_url('leescore/order/pay')}";
			var address = $("#address_id");
			var paytype = $("input[checked=checked]").val();
			if(address.length > 0 && address.val() == ''){
				$("#address_id").focus();
				layer.msg("{:__('art empty tip')}");
				return false;
			}
			if('{$item["paytype"]}' != '0')
			{
				layer.confirm('本次购买将消耗'+ ('{$item["score_total"]}')+"积分, "+ ('{$item["money_total"]}') +"元，是否继续购买？( 购买成功后需等待管理员发货。 )", {
					title: "消费确认",
					btn: ['现在支付','不了，我再看看'] //按钮
				}, function(){
					$.ajax({
						url: aurl,
						type: 'post',
						dataType: 'json',
						data: {oid: '{$item["id"]}' ,other: other, address: address.val()},

						success: function(json){
							if(json.code == true)
							{
								layer.confirm(json.msg, {
									title: '下单成功提示',
									closeBtn: false,
									btn: ['{:__("yes")}'] //按钮
								}, function(){
									parent.location.href = "{:addon_url('leescore/order/index')}";
								});
							}else{
								layer.alert(json.msg, {
									icon: 2,
									skin: 'layer-ext-moon'
								});
							}
						}
					});
				}, function(){
					layer.close();
				});
				//$("#postorder").attr("action",aurl);
				//$("#postorder").submit();
			}else{
				layer.confirm('本次购买将消耗'+ ('{$item["score_total"]}')+"积分，是否继续购买？", {
					title: "消费确认",
					btn: ['现在支付','不了，我再看看'] //按钮
				}, function(){
					$.ajax({
						url: aurl,
						type: 'post',
						dataType: 'json',
						data: {oid: '{$item["id"]}' ,other: other, address: address.val()},

						success: function(json){
							if(json.code == true)
							{
								layer.confirm(json.msg, {
									title: '下单成功提示',
									btn: ['{:__("yes")}','{:__("no")}'] //按钮
								}, function(){
									parent.location.href = "{:addon_url('leescore/order/index')}";
								});
							}else{
								layer.alert(json.msg, {
									icon: 2,
									skin: 'layer-ext-moon'
								});
							}
						}
					});
				}, function(){
					layer.close();
				});
				
			}
		});


		$('.edit-btn').on('click', function() {
			var bwith = $("body").width();
    		var id = $(this).attr('data-id');
    		var w = bwith < 768 ? '100%' : '100%';
    		var eurl = '{:addon_url("leescore/address/edit")}';
    		eurl = eurl + "?id="+ id;
			parent.layer.open({
				type: 2,
				title: '{:__("address")}{:__("edit")}',
				area: ['70%','90%'],
				shadeClose: true,
				cancel: function(){
					//window.location.reload();
				},
				shade: 0.7,
				//area: [w, w],
				content: eurl //iframe的url
			});
		});

		var defaultAddress = $(".goods-order-address li.active").length;
		var adds = $(".goods-order-address li.item").length;
		if(defaultAddress <= 0 && adds > 0)
		{
			$(".goods-order-address li.item").eq(0).addClass('active');
			var addid = $(".goods-order-address li.item").eq(0).attr('data-param');
			$("#address_id").val(addid);
		}
		
		$('.goods-order-address .item').on('click', function() {
			$('.goods-order-address .item').removeClass('active');
			$(this).addClass('active');
			$("#address_id").val($(this).attr("data-param"));
		});
	});
</script>
